/**
 * 全站路由配置
 *
 * meta参数说明
 * keepAlive是否缓冲页面
 * isTab是否加入到tag导航
 * isAuth是否需要授权
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由导航冗余报错（路由重复）
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
Vue.use(VueRouter)

export const constantRoutes = [
  {
    path: '/select',
    name: 'SelectApp',
    redirect: '/select/list',
    component: () => import('@index/views/homeIndex/appRedirect'),
    children: [
      {
        path: 'list',
        name: '列表数据',
        meta: {},
        component: (resolve) => require(['@index/views/homeIndex/list'], resolve)
        // component: () => import(/* webpackChunkName: "views" */ '@index/views/homeIndex/list')
      },
      {
        path: 'detail',
        name: '列表数据',
        meta: {},
        component: (resolve) => require(['@index/views/homeIndex/detail'], resolve)
        // component: () => import(/* webpackChunkName: "views" */ '@index/views/homeIndex/detail')
      }
    ]
  }
]
// 创建路由
export const createRouter = () =>
  new VueRouter({
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition
      } else {
        if (from.meta.keepAlive) {
          from.meta.savedPosition = document.body.scrollTop
        }
        return {
          x: 0,
          y: to.meta.savedPosition || 0
        }
      }
    },
    routes: [...constantRoutes]
  })
const Router = createRouter() // 获得 route 实例
export function resetRouter() {
  // 重置路由 比如用于身份验证失败，需要重新登录时 先清空当前的路有权限
  const newRouter = createRouter()
  Router.matcher = newRouter.matcher // reset router
}
export default Router
